import 'package:app/pages/car_modules/car_view.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:get/get.dart';

import '../../../router/app_pages.dart';
import 'package:intl/intl.dart' as Datee;

class ApplyCarController extends GetxController{


  var carName = '选择车辆';
  var carID;

  // var userName = '选择使用人';
  // var userId;


  List<String> userIdName = ['请选择使用人'];
  List<int>? userId;


  var startTime = '开始时间';
  var endTime = '结束时间';

  var urgency = '加急';

  TextEditingController placeStartController = TextEditingController();
  TextEditingController placeEndController = TextEditingController();

  tapClickCar()async{
    var data = await Get.to(CarView());
    if(data != null){
      carName = data['name'];
      carID = data['id'];
    }
    update();
  }

  tapClickUser()async{

    var params = {
      'isMultiple':false,
      'personnelIdArr':userId,
      // 'personnelNameArr':userIdName
    };

    var data = await Get.toNamed(AppRoutes.personnel,arguments: params);
    print('data======${data}');
    if(data != null){
      print(data);
      userIdName.remove('请选择使用人');
      userIdName = data['personnelNameArr'];
      userId = data['personnelIdArr'];
      update();
    }

    // var params = {
    //   'isMultiple':false,
    //   'personnelIdArr':userId,
    //   // 'personnelNameArr':userIdName
    // };
    //
    // var data = await Get.toNamed(AppRoutes.personnel,arguments: params);
    //
    // if(data != null){
    //   print(data);
    //   userIdName = data['personnelNameArr'][0];
    //   userId = data['personnelIdArr'][0];
    // }
    // update();
  }

  /// start Time;
  tapClickStartTime()async{
    DatePicker.showDateTimePicker(Get.overlayContext!,
        showTitleActions: true,
        minTime: DateTime(2000,11,11),
        onConfirm: (date){
          var timeFormat = Datee.DateFormat("yyyy-MM-dd HH:mm");
          var timeStr = timeFormat.format(date);
          startTime = timeStr;
        },
        currentTime: DateTime.now(),
        locale: LocaleType.zh
    );
    update();
  }
  /// endTime
  tapClickEndTime()async{

    print(DateTime.parse(startTime));
    DatePicker.showDateTimePicker(Get.overlayContext!,
        showTitleActions: true,
        minTime: DateTime.parse(startTime),
        // maxTime: DateTime.parse(startTime),
        onConfirm: (date){
          var timeFormat = Datee.DateFormat("yyyy-MM-dd HH:mm");
          var timeStr = timeFormat.format(date);
          endTime = timeStr;
        },
        currentTime: DateTime.parse(startTime),
        locale: LocaleType.zh
    );
    update();
  }


  /// 点击附件icon
  tapClickAccessory(List accessary) {
    List<BrnCommonActionSheetItem> actions = [];
    accessary.forEach((element) {
      actions.add(BrnCommonActionSheetItem(
        element,
        actionStyle: BrnCommonActionSheetItemStyle.normal,
      ));
    });
    showModalBottomSheet(
        context: Get.overlayContext!,
        backgroundColor: Colors.transparent,
        builder: (BuildContext context) {
          return BrnCommonActionSheet(
            title: "",
            actions: actions,
            clickCallBack: (
                int index,
                BrnCommonActionSheetItem actionEle,
                ) async {
              print("tapClickAccessory===>>>$index");
              urgency = accessary[index];
              urgency = accessary[index]=='正常'?'正常'
                  :accessary[index]=='加急'?'加急':'特急';
              update();
            },
          );
        });
  }
  
  requestDataWithCreate()async{
    var params = {
      'car_id':carID,
      'user_id':userId![0],
      'car_case_id':'0',
      'start_time':'${startTime}',
      'end_time':endTime,
      'place_start':placeStartController.text,
      'place_end':placeEndController.text,
      'urgency':urgency, //紧急程度，正常，加急，特急
    };
    var json = await DioManager().kkRequest(Address.oaCarsCasesWrite,bodyParams: params);
    if(json['errmsg'] == 'ok'){
      Get.back(result: 'refresh');
    }
    BotToast.showText(text: json['message']);
  }
}